<?PHP
/*
 * $Id: bootstrap.inc.php 139 2009-12-16 15:29:56Z namba0219 $
 * =============================================================================
 * Barge - Web application framework on PHP5
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
require_once dirname(__FILE__). '/barge.classes.php';

// Attach auto class loading fuction.
function __autoload($name)
{
	$classLoader = barge\System::getClassLoader();
	$classLoader->loadClass($name);
}

$classLoader = barge\System::getClassLoader();
$classLoader->addIncludePath(dirname(__FILE__).'/../');

function _errorhandler($errno, $errstr, $errfile = null, $errline = null, array $contxt = null)
{
	// Note : @演算子を使用している場合はスキップする
	if (error_reporting() !== 0) {
		$errorHandler = barge\System::getErrorHandler();
		$errorHandler->uncoughtableError($errno, $errstr, $errfile, $errline, $contxt);
	}
	// $php_errormsg を設定させるために<code>false</code>
	return false;
}

// Invoke main class.
function _exceptionhandler($exception)
{
	$errorHandler = barge\System::getErrorHandler();
	$errorHandler->uncoughtableException($exception);
}

function _shutdownhandler()
{
	if ($error = error_get_last()) {
		switch($error['type']){
    	case E_ERROR:
        case E_PARSE:
        case E_CORE_ERROR:
        case E_CORE_WARNING:
        case E_COMPILE_ERROR:
        case E_COMPILE_WARNING:
        	_errorhandler($error['type'], $error['message'], $error['file'], $error['line'], null);
			break;
		}
	}
}


//if (isset($_SERVER['argc']) && $_SERVER['argc'] > 0) {
//	$module = $_SERVER['argv'][0];
//	$p = strpos($module, '.');
//	if ($p !== false) {
//		register_shutdown_function('_invokemain', substr($module, 0, $p));
//	}
//}
//function _invokemain($class)
//{
//	$reflectionClass = new ReflectionClass($class);
//	$reflectionMethod = $reflectionClass->getMethod('main');
//	$reflectionMethod->invoke(null, array_slice($_SERVER['argv'], 1));
//}
// Invoke main class.
function register_main_class($class)
{
//	if (isset($_SERVER['argc']) && $_SERVER['argc'] > 0) {
//		register_shutdown_function('_invokemain', $class);
//	}
//	system('chcp 65001');
	$reflectionClass = new ReflectionClass($class);
	$reflectionMethod = $reflectionClass->getMethod('main');
	$reflectionMethod->invoke(null, array_slice($_SERVER['argv'], 1));
}

//set_error_handler('_errorhandler', E_ALL | E_STRICT);
set_error_handler('_errorhandler');
set_exception_handler('_exceptionhandler');
register_shutdown_function( '_shutdownhandler' );
ini_set('track_errors', 1);	// $php_errormsg
?>